package com.demo07;

import com.google.common.collect.Table;
import com.google.common.collect.TreeBasedTable;

import java.util.ArrayList;
import java.util.Collections;


/**
 * 实现了按照group by rowkey, reduce by value, sort by value;
 *
 * @author leebin
 */
public class RcvList {
    private ArrayList<RcvBean> list;
    private TreeBasedTable<String, String, Integer> tbt;

    /**
     * 构造
     */
    public RcvList() {
        list = new ArrayList<>(0);
        tbt = TreeBasedTable.create();
    }

    /**
     * put 加入三元组
     *
     * @param rowKey
     * @param column
     * @param value
     * @return
     */
    public RcvList put(String rowKey, String column, Integer value) {
        if (!tbt.contains(rowKey, column)) {
            tbt.put(rowKey, column, value);
        } else {
            tbt.put(rowKey, column, value + tbt.get(rowKey, column));
        }
        return this;
    }

    /**
     * get 得到list
     *
     * @return
     */
    public ArrayList<RcvBean> get() {
        if (list.size() == 0) {
            return list;
        } else {
            sort();
            return list;
        }
    }

    /**
     * 打印到文件或者控制台
     *
     * @param prefix   前缀
     * @param filepath 文件路径
     */
    public void printRcvList(String prefix, String... filepath) {
        if (list.size() == 0) {
            sort();
        }
        String line;
        for (RcvBean rcvBean : list) {
            String rowKey = rcvBean.getRowKey();
            String column = rcvBean.getColumn();
            Integer value = rcvBean.getValue();
            if (prefix == null) {
                line = rowKey + "\t" + column + "\t" + value;
            } else {
                line = prefix + "\t" + rowKey + "\t" + column + "\t" + value;
            }
            if (filepath.length == 0) {
                System.out.println(line);
            } else {
                FileUtilsHelper.write(filepath[0], line);
            }
        }
    }

    private void sort() {
        list.clear();
        for (Table.Cell<String, String, Integer> cell : tbt.cellSet()) {
            RcvBean rcvBean = new RcvBean(cell.getRowKey(), cell.getColumnKey(), cell.getValue());
            list.add(rcvBean);
        }
        Collections.sort(list);
    }

    class RcvBean implements Comparable<RcvBean> {
        private String rowKey;
        private String column;
        private Integer value;

        @Override
        public String toString() {
            return rowKey + "\t" + column + "\t" + value;
        }

        public String getRowKey() {
            return rowKey;
        }

        public void setRowKey(String rowKey) {
            this.rowKey = rowKey;
        }

        public String getColumn() {
            return column;
        }

        public void setColumn(String column) {
            this.column = column;
        }

        public Integer getValue() {
            return value;
        }

        public void setValue(Integer value) {
            this.value = value;
        }

        public RcvBean(String rowKey, String column, Integer value) {
            this.rowKey = rowKey;
            this.column = column;
            this.value = value;
        }

        /**
         * 排序方法
         *
         * @param o
         * @return
         */
        @Override
        public int compareTo(RcvBean o) {
            String rowKey = o.getRowKey();
            String column = o.getColumn();
            Integer value = o.getValue();
            if (this.rowKey.compareTo(rowKey) > 0) {
                return 1;
            } else if (this.rowKey.compareTo(rowKey) < 0) {
                return -1;
            } else {
                return -this.value.compareTo(value);
            }
        }
    }
}
